package org.qiyu.live.api.service.impl;

import org.apache.dubbo.config.annotation.DubboReference;
import org.qiyu.live.api.error.ApiErrorEnum;
import org.qiyu.live.api.service.IShopInfoService;
import org.qiyu.live.api.vo.PrepareOrderVO;
import org.qiyu.live.api.vo.req.ShopCarReqVO;
import org.qiyu.live.api.vo.req.SkuInfoReqVO;
import org.qiyu.live.api.vo.resp.ShopCarItemRespVO;
import org.qiyu.live.api.vo.resp.ShopCarRespVO;
import org.qiyu.live.api.vo.resp.SkuDetailInfoVO;
import org.qiyu.live.api.vo.resp.SkuInfoVO;
import org.qiyu.live.common.interfaces.utils.ConvertBeanUtils;
import org.qiyu.live.interfaces.sku.dto.*;
import org.qiyu.live.interfaces.sku.rpc.IShopCarRPC;
import org.qiyu.live.interfaces.sku.rpc.ISkuOrderInfoRPC;
import org.qiyu.live.interfaces.sku.rpc.ISkuRPC;
import org.qiyu.live.living.interfaces.dto.LivingRoomRespDTO;
import org.qiyu.live.living.interfaces.rpc.ILivingRoomRpc;
import org.qiyu.live.web.starter.context.QiyuRequestContext;
import org.qiyu.live.web.starter.error.BizBaseErrorEnum;
import org.qiyu.live.web.starter.error.ErrorAssert;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Program: qiyu-live-app
 *
 * @Description:
 *
 * @Author: zx
 *
 * @Create: 2024-08-15 10:01
 */
@Service
public class ShopInfoServiceImpl implements IShopInfoService {

    @DubboReference
    ILivingRoomRpc livingRoomRpc;

    @DubboReference
    ISkuRPC skuRpc;

    @DubboReference
    IShopCarRPC shopCarRPC;
    @DubboReference
    ISkuOrderInfoRPC skuOrderInfoRPC;

    @Override
    public List<SkuInfoVO> queryByRoomId(Integer roomId) {
        LivingRoomRespDTO livingRoomRespDTO = livingRoomRpc.queryByRoomId(roomId);
        ErrorAssert.isNotNull(livingRoomRespDTO, BizBaseErrorEnum.PARAM_ERROR);
        List<SkuInfoDTO> skuInfoDTOList = skuRpc.queryByAnchorId(livingRoomRespDTO.getAnchorId());
        ErrorAssert.isTure(!CollectionUtils.isEmpty(skuInfoDTOList), BizBaseErrorEnum.PARAM_ERROR);
        return ConvertBeanUtils.convertList(skuInfoDTOList, SkuInfoVO.class);
    }

    @Override
    public SkuDetailInfoVO detail(SkuInfoReqVO reqVO) {
        return ConvertBeanUtils.convert(skuRpc.queryBySkuId(reqVO.getSkuId()), SkuDetailInfoVO.class);
    }

    @Override
    public ShopCarRespVO getCarInfo(ShopCarReqVO reqVO) {
        ShopCarReqDTO reqDTO = ConvertBeanUtils.convert(reqVO, ShopCarReqDTO.class);
        reqDTO.setUserId(QiyuRequestContext.getUserId());
        ShopCarRespDTO shopCarRespDTO = shopCarRPC.getCarInfo(reqDTO);
        ShopCarRespVO shopCarRespVO = ConvertBeanUtils.convert(shopCarRespDTO, ShopCarRespVO.class);
        shopCarRespVO.setShopCarItemRespVOList(ConvertBeanUtils.convertList(shopCarRespDTO.getShopCarItemRespDTOList(), ShopCarItemRespVO.class));
        return shopCarRespVO;
    }

    @Override
    public boolean addCar(ShopCarReqVO reqVO) {
        ShopCarReqDTO reqDTO = ConvertBeanUtils.convert(reqVO, ShopCarReqDTO.class);
        reqDTO.setUserId(QiyuRequestContext.getUserId());
        return shopCarRPC.addCar(reqDTO);
    }

    @Override
    public boolean removeFromCar(ShopCarReqVO reqVO) {
        ShopCarReqDTO reqDTO = ConvertBeanUtils.convert(reqVO, ShopCarReqDTO.class);
        reqDTO.setUserId(QiyuRequestContext.getUserId());
        return shopCarRPC.removeFromCar(reqDTO);
    }

    @Override
    public boolean clearCar(ShopCarReqVO reqVO) {
        ShopCarReqDTO reqDTO = ConvertBeanUtils.convert(reqVO, ShopCarReqDTO.class);
        reqDTO.setUserId(QiyuRequestContext.getUserId());
        return shopCarRPC.clearCar(reqDTO);
    }

    @Override
    public boolean addCarItemNum(ShopCarReqVO reqVO) {
        ShopCarReqDTO reqDTO = ConvertBeanUtils.convert(reqVO, ShopCarReqDTO.class);
        reqDTO.setUserId(QiyuRequestContext.getUserId());
        return shopCarRPC.addCarItemNum(reqDTO);
    }

    @Override
    public SkuPrepareOrderInfoDTO prepareOrder(PrepareOrderVO reqVO) {
        reqVO.setUserId(QiyuRequestContext.getUserId());
        return skuOrderInfoRPC.prepareOrder(ConvertBeanUtils.convert(reqVO, PrepareOrderReqDTO.class));
    }

    @Override
    public boolean payNow(PrepareOrderVO prepareOrderVO) {
        prepareOrderVO.setUserId(QiyuRequestContext.getUserId());
        boolean isSuccess = skuOrderInfoRPC.payNow(ConvertBeanUtils.convert(prepareOrderVO, PayNowReqDTO.class));
        ErrorAssert.isTure(isSuccess, ApiErrorEnum.PAY_ERROR);
        return isSuccess;
    }
}
